from bisect import bisect_left
from typing import List


class Solution:
    def minEatingSpeed(self, piles: List[int], h: int) -> int:
        left, right = 1, max(piles)
        ans = right
        while left <= right:
            mid = (left + right) // 2
            if self.check(piles, h, mid):
                ans = mid
                right = mid - 1
            else:
                left = mid + 1
        return ans
    
    def check(self, piles: List[int], h: int, mid: int) -> bool:
        time = 0
        for pile in piles:
            time += (pile + mid - 1) // mid
        return time <= h


class Solution2:
    def minEatingSpeed(self, piles: List[int], h: int) -> int:
        return bisect_left(range(max(piles)), -h, 1, key=lambda k: -sum((pile + k - 1) // k for pile in piles))
